import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class countDownLatch {

    private static final int threadCount = 500;

    public static void main(String[] args) throws InterruptedException{

        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
              100, 200, 1L, TimeUnit.SECONDS,
              new ArrayBlockingQueue<>(500),
              new ThreadPoolExecutor.AbortPolicy()
        );

        // 一次性的，计数值只能在构造方法中初始化一次
        final CountDownLatch countDownLatch = new CountDownLatch(threadCount);

        for (int i = 0; i < threadCount; i++) {
            final int threadNum = i;
            threadPool.execute(() -> {
                try {
                    test(threadNum);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                finally {
                    // 一个请求完成
                    countDownLatch.countDown();
                }
            });
        }
        // 在这停顿
        countDownLatch.await();
        threadPool.shutdown();
        System.out.println("finished!");
    }

    public static void test (int threadNum) throws InterruptedException {
        Thread.sleep(1000);
        System.out.println("ThreadNum: " + threadNum);
        Thread.sleep(1000);
    }

}
